package com.bowen.spike.controller;


import com.bowen.spike.common.Result;
import com.bowen.spike.context.UserContext;
import com.bowen.spike.entity.UserVoucher;
import com.bowen.spike.service.IUserVoucherService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户优惠券关联表 前端控制器
 * </p>
 *
 * @author bowen
 * @since 2025-04-28
 */
@RestController
@RequestMapping("/api/user-voucher")
@RequiredArgsConstructor
public class UserVoucherController {

    private final IUserVoucherService userVoucherService;


    /**
     * 秒杀优惠劵
     */
    @GetMapping("/go")
    public Result lightningDealCoupons(@RequestParam Long voucherId) {
        return userVoucherService.lightningDealCoupons(voucherId);
    }

    /**
     * 查询用户优惠券关联记录
     */
    @GetMapping
    public Result<List<UserVoucher>> getAllUserVouchers() {
        List<UserVoucher> voucherList = userVoucherService.lambdaQuery()
                .eq(UserVoucher::getUserId, UserContext.getUserId())
                .list();
        return Result.ok(voucherList);
    }

    /**
     * 根据ID查询用户优惠券关联记录
     */
    @GetMapping("/{id}")
    public Result<UserVoucher> getUserVoucherById(@PathVariable Long id) {
        UserVoucher userVoucher = userVoucherService.getById(id);
        return Result.ok(userVoucher);
    }

    /**
     * 更新用户优惠券状态
     */
    @PutMapping("/{status}")
    public Result<Void> updateUserVoucher(@PathVariable Integer status) {
        userVoucherService.lambdaUpdate()
                .eq(UserVoucher::getUserId, UserContext.getUserId())
                .update();
        return Result.ok();
    }

    /**
     * 删除所有优惠券
     */
    @DeleteMapping
    public Result<Void> deleteVoucher() {
        userVoucherService.lambdaUpdate().remove();
        return Result.ok();
    }
}
